//
//  KRGarageViewController.m
//  KEREN
//
//  Created by MW on 2023/4/11.
//

#import "KRGarageViewController.h"
#import "GaragelistViewController.h"
#import "ScanDeviceViewController.h"
@interface KRGarageViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *btnArr;
@property (nonatomic, strong) UIView *currentLineView;
@property (nonatomic, strong) GaragelistViewController *csVC;
@property (nonatomic, strong) GaragelistViewController *tlVC;
@property (nonatomic, strong) GaragelistViewController *crVC;

@end

@implementation KRGarageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavView];
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 55 + STATUSBAR_HEIGHT, DEVICE_WIDTH, DEVICE_HEIGHT - 55 - STATUSBAR_HEIGHT)];
    [self.view addSubview:_scrollView];
    _csVC = [[GaragelistViewController alloc] init];
    [_csVC requsetDeviceInfoList];
    _tlVC = [[GaragelistViewController alloc] init];
    _crVC = [[GaragelistViewController alloc] init];
    [self addChildViewController:_csVC];
    [self addChildViewController:_tlVC];
    [self addChildViewController:_crVC];
    
    _csVC.view.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
    _tlVC.view.frame = CGRectMake(DEVICE_WIDTH, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
    _crVC.view.frame = CGRectMake(DEVICE_WIDTH * 2, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
    
    [_scrollView addSubview:_csVC.view];
    [_scrollView addSubview:_tlVC.view];
    [_scrollView addSubview:_crVC.view];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(DEVICE_WIDTH * 3, 0);
    _scrollView.delegate = self;
    if(_type > 0){
        [self updateTopView:_type];
        [self.scrollView setContentOffset:CGPointMake(DEVICE_WIDTH * _type, 0) animated:NO];
    }
}

- (void)setNavView{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 56 + STATUSBAR_HEIGHT)];
    [self.view addSubview:navView];
    navView.backgroundColor = Whire_Color;
    UIButton *backBtn = [[UIButton alloc] init];
    [navView addSubview:backBtn];
    [backBtn setImage:[UIImage imageNamed:@"main_back"] forState:UIControlStateNormal];

    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(24);
        make.top.offset(8 + STATUSBAR_HEIGHT);
        make.width.height.offset(40);
    }];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *addBtn = [[UIButton alloc] init];
    [navView addSubview:addBtn];
    [addBtn setImage:[UIImage imageNamed:@"devcie_add_icon"] forState:UIControlStateNormal];

    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-24);
        make.top.offset(8 + STATUSBAR_HEIGHT);
        make.width.height.offset(40);
    }];
    [addBtn addTarget:self action:@selector(addDevcie) forControlEvents:UIControlEventTouchUpInside];
    
    NSArray *titleArr = @[@"车库",@"分享",@"接受"];
    for (int i = 0; i < titleArr.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(80 + (DEVICE_WIDTH - 160)/3 * i, 8 + STATUSBAR_HEIGHT, (DEVICE_WIDTH - 160)/3, 40)];
        [navView addSubview:btn];
        if(i == 0){
            btn.titleLabel.font = BFont(20);
            [btn setTitleColor:Color_7ACC00 forState:UIControlStateNormal];
            _currentLineView = [[UIView alloc] init];
            [navView addSubview:_currentLineView];
            [_currentLineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(20);
                make.height.offset(2);
                make.centerX.equalTo(btn);
                make.bottom.offset(-1);
            }];
            _currentLineView.backgroundColor = Color_7ACC00;
        }else{
            btn.titleLabel.font = Font_Medium(16);
            [btn setTitleColor:Color_86898E forState:UIControlStateNormal];
        }
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnArr addObject:btn];
    }
    [UIView setLineViewWithframe:CGRectMake(0, 55 + STATUSBAR_HEIGHT, DEVICE_WIDTH, 1) forinView:navView];

}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)btnClick:(UIButton *)sender{
    [self updateTopView:sender.tag];
    [self.scrollView setContentOffset:CGPointMake(DEVICE_WIDTH * sender.tag, 0) animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    float x = scrollView.contentOffset.x + DEVICE_WIDTH /2;
    int index = x / DEVICE_WIDTH ;
    [self updateTopView:index];
}

- (void)addDevcie{
    ScanDeviceViewController *vc = [[ScanDeviceViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)updateTopView:(NSInteger)tag{
    for (UIButton *btn in _btnArr) {
        if(btn.tag == tag){
            btn.titleLabel.font = BFont(20);
            [btn setTitleColor:Color_7ACC00 forState:UIControlStateNormal];
            [_currentLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(20);
                make.height.offset(2);
                make.centerX.equalTo(btn);
                make.bottom.offset(-1);
            }];
        }else{
            btn.titleLabel.font = Font_Medium(16);
            [btn setTitleColor:Color_86898E forState:UIControlStateNormal];
        }
    }
    if(tag == 0){
        [_csVC requsetDeviceInfoList];
    }else if (tag == 1){
        [_tlVC getDeviceUserShareList];
    }else{
        [_crVC getDevicesharelist];
    }
}

- (NSMutableArray *)btnArr{
    if(!_btnArr){
        _btnArr = [NSMutableArray array];
    }
    return _btnArr;
}


@end
